package ONLINE_EXAM;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class test2 {
    /**
     * 思路 动态规划
     * dp[j] 寻找第j个元素的最短距离
     * base case
     * dp[0] = index[j=0]与start之间的最小数
     * @param args
     */

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = br.readLine())!= null){
            HashMap<Integer,Character> map = new HashMap<>();

            for (int i=0;i<str.length();i++){
                map.put(i,str.charAt(i));

            }

            String mask = br.readLine();
            String s = br.readLine();
            int start = s.charAt(0)-48;
            int res = helper(map,mask,start);
            System.out.println(res);
        }
    }
    public  static int helper(HashMap<Integer,Character> map,String mask,int start){

        int res =0;
        int[] dp = new int[mask.length()];
        //找到第一个元素
        dp[0] = Integer.MAX_VALUE;
        int index =0;
        for (int i = 0;i<map.size();i++){
            if (map.get(i).equals(mask.charAt(0))){
                dp[0]= Math.min(dp[0],Math.abs(i-start) );
                index = i;
            }
        }

        for (int j = 1; j < mask.length(); j++) {
            //index 上一个位子
            dp[j] = dp[j-1]+ minDis(index,mask.charAt(j),map);

        }
        return dp[mask.length()-1];

    }
    public static int minDis(int start,char val,HashMap<Integer,Character> map){
        int res = Integer.MAX_VALUE;
        for (int i = 0;i<map.size();i++){
            if (map.get(i).equals(val)){
                res = Math.min(res,Math.abs(i-start) );
            }
        }
        return res;
    }
}
